import {uSelectDevProductFile} from './../../../api/setting'

import {observable, computed, action} from 'mobx'
import {createContext} from 'react'
import ChildrenPageUtils from '@/store/extends'
import Taro from '@tarojs/taro'

enum PDFType {
  instructionBook = '0045',
  video = '0046',
  devTypeImg = '0153'
}

class Productfiles extends ChildrenPageUtils {
  @observable fileList: Setting.FileData[] = []

  @action.bound
  async updateFileList() {
    if (!this.pageQuery.devId) return
    let res = await uSelectDevProductFile({devId: this.pageQuery.devId})
    if (res && res.result === 'success') {
      if (res.data.length) {
        this.fileList = res.data
      } else {
        Taro.showModal({
          content: '暂无产品资料'
        })
        this.fileList = []
      }
    } else {
      this.fileList = []
    }
  }

  @action.bound
  returnTypeNameByTypeCode(type: string) {
    switch (type) {
      case PDFType.instructionBook:
        return '电子说明书'
      case PDFType.video:
        return '产品资料'
      case PDFType.devTypeImg:
        return '产品类型图片'
      default:
        return '未知文件类型'
    }
  }

  @action.bound
  clearPageData() {
    this.fileList = []
  }
}
export const productFilesStore = createContext(new Productfiles())
